﻿using System.Linq;
using DevExpress.XtraBars;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Views.Base;
using NAGFwk.BL;
using NAGFwk.UI;
using SilkeyERP.BL;
using SilkeyERP.Model;

namespace SilkeyERP.UI.Entities.Provinces
{
    public partial class ContentView : BaseContentDetail
    {
        private IBaseController<Province> _controller = new ProvinceController();

        public ContentView()
        {
            InitializeComponent();
        }

        public override void ShowModule(bool firstShow)
        {
            base.ShowModule(firstShow);
            MapItLoad(_controller);
            countryBindingSource.DataSource = new CountryController().GetAll();
        }

        private void RefreshAll()
        {
            _controller = MapItRefresh(new ProvinceController());
        }

        private void bbiDelete_ItemClick(object sender, ItemClickEventArgs e)
        {
            MapItDelete(_controller);
        }

        private void gvBaseContentDetail_RowUpdated(object sender, RowObjectEventArgs e)
        {
            MapItUpdate(e, _controller);
        }

        private void bbiRefresh_ItemClick(object sender, ItemClickEventArgs e)
        {
            RefreshAll();
        }

        protected override void ValidateRows(ValidateRowEventArgs e)
        {
            var typedRow = ((Province)e.Row);
            if (string.IsNullOrEmpty(typedRow.Description))
            {
                e.ErrorText = "Descripcion es Requerido.";
                e.Valid = false;
            }

            if (typedRow.CountryID == int.MinValue)
            {
                e.ErrorText = "Ciudad es Requerido.";
                e.Valid = false;
            }

            
        }

    }
}
